package com.example.automanagement.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.automanagement.dto.RepairApprovalDto;
import com.example.automanagement.entity.AutoArchives;
import com.example.automanagement.entity.AutoGeneralApproval;
import com.example.automanagement.entity.RepairGeneralApproval;
import com.example.automanagement.service.ArchiveService;
import com.example.automanagement.service.AutoStatusService;
import com.example.automanagement.service.RepairApprovalService;
import com.example.automanagement.util.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author Ren.Yuhong
 * 处理维修工单的综合办审批 ，与用车工单对应
 * */
@RestController
public class RepairApprovalController {
    @Resource
    private RepairApprovalService repairApprovalService;
    @Resource
    private ArchiveService autoArchiveService;
    @Resource
    private AutoStatusService autoStatusService;


    @PostMapping("/addRepairGeneralApproval")
    public Result addRepairGeneralApproval(@RequestBody RepairApprovalDto approval){
        if(approval == null)
            return Result.error("500", "请检查数据 ");
        //添加维修工单综合办审批，先提取对象
        RepairGeneralApproval repairGeneralApproval = BeanUtil.copyProperties(approval, RepairGeneralApproval.class);
        //再插入
        repairApprovalService.addRepairApproval(repairGeneralApproval);
        //取车牌号
        if(approval.getPlateNum() == null)
            return Result.error("500", "请检查数据 ");
        //寻找对应车辆
        AutoArchives auto = autoArchiveService.getAutoIdByPlate(approval.getPlateNum());
        if(auto.getAutoId() == null)
            return Result.error("500","车辆不存在");
        //设为维修
        autoStatusService.updateAutoRepairing(auto.getAutoId());
        return Result.success();
    }
    @GetMapping("/getRepairGeneralApproval")
    public Result getRepairGeneralApproval(@RequestParam("orderId") String orderId){
        RepairGeneralApproval approval = repairApprovalService.getRepairGeneralApproval(orderId);
        return Result.success(approval,"查询成功");
    }
}
